import axios from 'axios'
import baseUrl from '../../util/url.js'
const url = baseUrl + 'api.php/records/shopcar'
// const url1 = baseUrl + 'api.php/shopcar'
export default {
    namespaced: true,
    state: {
        list: [],
        count:0
    },
    mutations: {},
    actions: {
        getlist({ state, commit, dispatch }, id) {
            return new Promise((resolve, reject) => {
                axios({
                    method: 'get',
                    url,
                    params: { transform: 1 , filter:'uid,eq,'+id}
                }).then(res => {
                    state.list = res.data.records
                    resolve(res.data.records)
                }).catch(e => {
                    reject()
                })
            });
        },
        updateList({ state, commit, dispatch }, obj) {
            return new Promise((resolve, reject) => {
                axios({
                    method: 'put',
                    url:url + '/' + obj.id,
                    data:{
                        num:obj.num
                    }
                }).then(res => {
                    resolve()
                }).catch(e => {
                    reject()
                })
            });
        },
        deleteList(context, id) {
            return new Promise((resolve, reject) => {
                axios({
                    method: 'delete',
                    url:url + '/' + id,
                })
                .then(()=>{
                    resolve()
                })
            });
        },
        getShopNum({ state, commit, dispatch }) {
            return new Promise((resolve, reject) => {
                axios({
                    method: 'get',
                    url,
                    params: {}
                }).then(res => {
                    let arr=0
                    for(var i =0;i<res.data.records.length;i++){
                        arr += res.data.records[i].num*1
                    }
                    state.count = arr
                }).catch(e => {
                    reject()
                })
            });
        },
        
    }

}